package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ClientsModel;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class ClientDAOImpl implements ClientDAO {

    private EntityManager entityManager;

    public ClientDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public ClientsModel getClientById(Integer id) {
        return (ClientsModel) entityManager.createNamedQuery("ClientsModel.findByClientId").setParameter("clientId", id).getSingleResult();
    }

    @Override
    public EntityManager getEntityManager() {
        return this.entityManager;
    }

    @Override
    public void addClient(ClientsModel client) {
        entityManager.getTransaction().begin();
        entityManager.persist(client);
        entityManager.flush();
        entityManager.refresh(client);
        entityManager.getTransaction().commit();
    }
    
    @Override
    public void updateClient(ClientsModel client) {
        entityManager.getTransaction().begin();
        ClientsModel actual = getClientById(client.getClientId());
        entityManager.persist(actual);
        actual.setBlocked(client.getBlocked());
        actual.setCity(client.getCity());
        actual.setComments(client.getComments());
        actual.setEmail(client.getEmail());
        actual.setHouseNumber(client.getHouseNumber());
        actual.setLastName(client.getLastName());
        actual.setLocalNumber(client.getLocalNumber());
        actual.setName(client.getName());
        actual.setNipNumber(client.getNipNumber());
        actual.setPassword(client.getPassword());
        actual.setPhone(client.getPhone());
        actual.setStreet(client.getStreet());
        actual.setZipCode(client.getZipCode());

        entityManager.getTransaction().commit();
    }

    @Override
    public int getClientsModelSize() {
        return entityManager.createNamedQuery("ClientsModel.findAll").getResultList().size();
    }

    @Override
    public void removeClientById(Integer id) {
        ClientsModel rowToDelete = getClientById(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public List<ClientsModel> getAllClients() {
        return entityManager.createNamedQuery("ClientsModel.findAll").getResultList();
    }

    @Override
    public ClientsModel findLogin(String login, String password) {
        ClientsModel client = new ClientsModel();
        client.setName("testName");
        return new ClientsModel();
    }

    @Override
    public ClientsModel getClientByEmail(String email) {
        List<ClientsModel> clientsByEmail = entityManager.createNamedQuery("ClientsModel.findByEmail").setParameter("email", email).getResultList();
        if (clientsByEmail.size() > 0) {
            return clientsByEmail.get(0);
        }
        return null;
    }

}
